
package com.twohalf.mifty.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.thrift.TException;

import club.jmint.mifty.dao.Sql;
import club.jmint.mifty.log.MyLog;
import club.jmint.mifty.service.MiftyService;
import com.twohalf.mifty.service.gen.ReviewService.Iface;

public class ReviewServiceImpl extends MiftyService implements Iface {
	private IIdBuilder ib = new IndexBasedIdBuilder();
	private String dbName = "db_twohalf";
	private String reviewTable = dbName+".t_review";
	private Sql ddl = Sql.getInstance();

	/**
	 * Create a new review record
	 * @param fields: object_id,object_name,user_id,user_name,review_content
	 * @return id
	 */
	@Override
	public Map<String, String> reviewAdd(Map<String, String> params) throws TException {
		HashMap<String, String> resultMap = new HashMap<String, String>();
		
		//create a unique review id
		String id = Integer.toString(ib.generateReviewIdx());
		params.put("id", id);
		
		int erows = ddl.sqlCreate(reviewTable, params);
		if (erows<0){
			MyLog.logger.error("review add failed.");
			resultMap.put("errorCode", Integer.toString(ErrorCode.TWOHALF_DB_OPERATION_FAILED.getCode()));
			resultMap.put("errorInfo", ErrorCode.TWOHALF_DB_OPERATION_FAILED.getInfo());
			return resultMap;			
		}
		else if (erows==0){
		}
		else {
			MyLog.logger.info("added review: " + id);
		}
		
		resultMap.put("id", id);
		return resultMap;
	}

	/**
	 * Delete a review record by id
	 * @param id
	 * @return errorCode
	 */
	@Override
	public Map<String, String> reviewDelete(Map<String, String> params) throws TException {
		HashMap<String, String> resultMap = new HashMap<String, String>();
		String id = params.get("id");
		String condition = "id="+id;
		
		int erows = ddl.sqlDelete(reviewTable, condition);
		if (erows<0){
			MyLog.logger.error("review delete failed.");
			resultMap.put("errorCode", Integer.toString(ErrorCode.TWOHALF_DB_OPERATION_FAILED.getCode()));
			resultMap.put("errorInfo", ErrorCode.TWOHALF_DB_OPERATION_FAILED.getInfo());
			return resultMap;			
		}
		else if (erows==0){
			MyLog.logger.warn("review not exists: " + id);
			resultMap.put("errorCode", Integer.toString(ErrorCode.TWOHALF_OBJECT_NOT_EXIST.getCode()));
			resultMap.put("errorInfo", ErrorCode.TWOHALF_OBJECT_NOT_EXIST.getInfo());
			return resultMap;
		}
		else{
			MyLog.logger.info("deleted review: " + id);
		}
		
		return resultMap;

	}

	/**
	 * Update review info fields
	 * @param id
	 * @return errorCode
	 */
	@Override
	public Map<String, String> reviewUpdate(Map<String, String> params) throws TException {
		HashMap<String, String> resultMap = new HashMap<String, String>();
		String id = params.get("id");
		String condition = "id="+id;
		
		int erows = ddl.sqlUpdate(reviewTable, params, condition);
		if (erows<0){
			MyLog.logger.error("review update failed.");
			resultMap.put("errorCode", Integer.toString(ErrorCode.TWOHALF_DB_OPERATION_FAILED.getCode()));
			resultMap.put("errorInfo", ErrorCode.TWOHALF_DB_OPERATION_FAILED.getInfo());
			return resultMap;			
		} else if (erows==0){
			MyLog.logger.warn("review not exists: " + id);
			resultMap.put("errorCode", Integer.toString(ErrorCode.TWOHALF_OBJECT_NOT_EXIST.getCode()));
			resultMap.put("errorInfo", ErrorCode.TWOHALF_OBJECT_NOT_EXIST.getInfo());
			return resultMap;			
		} else {
			MyLog.logger.info("updated review: " + id);
		}
		
		resultMap.put("id", id);
		return resultMap;
	}

	/**
	 * Query review info fields by id
	 * @param id
	 * @return review fields
	 */
	@Override
	public Map<String, String> reviewInfoQuery(Map<String, String> params) throws TException {
		HashMap<String, String> resultMap = new HashMap<String, String>();
		String id = params.get("id");
		String condition = "id="+id;
		
		String fields = params.get("fields");
		String[] fieldlist = fields.split(",");
		
		List<Object[]> list = ddl.sqlSelect(reviewTable, fields, condition);
		if (list.isEmpty()){
			MyLog.logger.warn("review not exists: " + id);
			resultMap.put("errorCode", Integer.toString(ErrorCode.TWOHALF_OBJECT_NOT_EXIST.getCode()));
			resultMap.put("errorInfo", ErrorCode.TWOHALF_OBJECT_NOT_EXIST.getInfo());
			return resultMap;			
		}
		MyLog.logger.info("query review: " + id);
		
		Object[] obj = list.get(0);
		for (int i=0;i<fieldlist.length;i++){
			resultMap.put(fieldlist[i], Sql.getSqlResultFieldAsString(obj[i]));
		}
		return resultMap;
	}

	@Override
	public Map<String, String> reviewListQuery(Map<String, String> params) throws TException {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * Query given product's user review
	 * @param product_id product id
	 * @return review record list
	 */
	@Override
	public Map<String, String> reviewInfoQueryByProduct(Map<String, String> params) throws TException {
		HashMap<String, String> resultMap = new HashMap<String, String>();
		String id = params.get("product_id");
		String condition = "object_id=" + id;
		
		String fields = params.get("fields");
		String[] fieldlist = fields.split(",");
		
		List<Object[]> list = ddl.sqlSelect(reviewTable, fields, condition);
		if (list.isEmpty()){
			MyLog.logger.warn("reviews not exist.");
			resultMap.put("errorCode", Integer.toString(ErrorCode.TWOHALF_OBJECT_NOT_EXIST.getCode()));
			resultMap.put("errorInfo", ErrorCode.TWOHALF_OBJECT_NOT_EXIST.getInfo());
			return resultMap;			
		}
		MyLog.logger.info("query all review by product id: " + id);
		
		Object[] objarr = null;
		String resobj;
		for (int idx=0;idx<list.size();idx++){
			objarr = list.get(idx);
			resobj = "{\"" + fieldlist[0] + "\",\"" + Sql.getSqlResultFieldAsString(objarr[0]) + "\"";
			for (int i=1;i<fieldlist.length;i++){
				resobj = ",\"" + fieldlist[i] + "\",\"" + Sql.getSqlResultFieldAsString(objarr[i]) + "\"";
			}
			resobj = "}";
			resultMap.put("rec_"+idx, resobj);
		}
		return resultMap;

	}

	/**
	 * Query review records by user id
	 * @param user_id user id
	 * @return review record list
	 */
	@Override
	public Map<String, String> reviewInfoQueryByUser(Map<String, String> params) throws TException {
		HashMap<String, String> resultMap = new HashMap<String, String>();
		String id = params.get("user_id");
		String condition = "user_id=" + id;
		
		String fields = params.get("fields");
		String[] fieldlist = fields.split(",");
		
		List<Object[]> list = ddl.sqlSelect(reviewTable, fields, condition);
		if (list.isEmpty()){
			MyLog.logger.warn("reviews not exist.");
			resultMap.put("errorCode", Integer.toString(ErrorCode.TWOHALF_OBJECT_NOT_EXIST.getCode()));
			resultMap.put("errorInfo", ErrorCode.TWOHALF_OBJECT_NOT_EXIST.getInfo());
			return resultMap;			
		}
		MyLog.logger.info("query all review by user id: " + id);
		
		Object[] objarr = null;
		String resobj;
		for (int idx=0;idx<list.size();idx++){
			objarr = list.get(idx);
			resobj = "{\"" + fieldlist[0] + "\",\"" + Sql.getSqlResultFieldAsString(objarr[0]) + "\"";
			for (int i=1;i<fieldlist.length;i++){
				resobj = ",\"" + fieldlist[i] + "\",\"" + Sql.getSqlResultFieldAsString(objarr[i]) + "\"";
			}
			resobj = "}";
			resultMap.put("rec_"+idx, resobj);
		}
		return resultMap;

	}

}

